import { createRouter, createWebHistory } from 'vue-router'
import Index from '../views/index.vue'
import Login from '../views/login.vue'
import Article from '../views/article/article.vue'
import ArticleList from '../views/article/list.vue'
import ArticleAdd from '../views/article/add.vue'

import UserList from '../views/user/list.vue'
import UserAdd from '../views/user/add.vue'


import RoleList from '../views/role/list.vue'
import RoleAdd from '../views/role/add.vue'
import RoleEdit from '../views/role/edit.vue'

import MenuList from '../views/menu/list.vue'
import MenuAdd from '../views/menu/add.vue'
import MenuRole from '../views/menu/menuRole.vue'

const routes = [
  { path: '/login', name: 'Login', component: Login },
  { path: '/', name: 'Index', component: Index ,children:[
    {//文章管理
      path: 'article', component: Article, children: [
        { path: 'list', component: ArticleList },
        { path: 'add', component: ArticleAdd }
      ]
    },
    {//权限管理
      path: 'auth', component: Article, children: [

        { path: 'user/list', component: UserList },
        { path: 'user/add', component: UserAdd },

        { path: 'role/list', component: RoleList },
        { path: 'role/add', component: RoleAdd },
        { path: 'role/edit', component: RoleEdit },

        { path: 'menu/list', component: MenuList },
        { path: 'menu/add', component: MenuAdd },
        { path: 'menu/role', component: MenuRole },
      ]
    }
  ]},

]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
router.beforeEach((to, from,next) => {
  
  let token=sessionStorage.getItem('accessToken')
  if (token) {
    next()
  } else {
    if (to.path ==="/login"|| to.path ==='/register'){
      next()
    }else {
      next('/login')
    }
  }

})

export default router
